<!--
 * @Author: luosi 1297957337@qq.com
 * @Date: 2024-11-29 17:08:09
 * @LastEditors: luosi 1297957337@qq.com
 * @LastEditTime: 2025-01-07 16:57:53
 * @FilePath: \sr_system_manager_ui\src\views\achievementReward\assessmentReward\components\GuideSummary.vue
 * @Description: 指导学生科研项目结项统计列表
-->

<template>
  <div class="person-summary-container">
    <div class="search" v-if="showSearch">
      <SearchBar ref="searchBarRef" :components="conditionComponents" :hideRightArea="false"
        @search="refreshTableData" />
    </div>
    <div class="list">
      <TableForm ref="tableRef" :data="dataList" :columns="columns" :tableLoading="loading" :total-count="total"
        v-model:showSearch="showSearch" :showRowSelection="true" :showPagination="true" :showToolBar="true"
        :download="(params) => { exportListData(params) }" @refresh="refreshTableData">
        <template #default="{ record }">
          <el-button link type="primary" @click="handleDetail(record.id)">查看</el-button>
        </template>
      </TableForm>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getGuideStuListApi, exportGuideStuApi } from '@/api/resultsManagement/guideStudentApi';
import { GuidePageVO, PageBeanGuidePageVO } from '@/api/resultsManagement/types';
import { dictEnum } from '@/utils/constEnum';
import formateDate from '@/utils/formateDate';
import { downloadFile } from '@/utils/download'

const props = defineProps({
  year: {
    type: Number
  },
  rewardFlag: {
    type: Boolean
  }
})

const { proxy } = getCurrentInstance()

const dict = proxy.useDict(dictEnum.department, dictEnum.resultGuideStuSureLevel)

const router = useRouter()
const searchBarRef = ref()
const showSearch = ref(true)
// 请求参数
const parameters = ref({
  page: 1,
  size: 20,
  departmentId: '',
  codeOrName: '',
  businessDateArray: [],
  projectCodeOrName: '',
  recognitionLevel: '',
  approvedBy: '',
  issuingUnit: ''
})

// 导出参数
const downloadParameters = ref({
  statisticalYear: props.year,
  status: 5,
  researchScoreFlag: true,
  rewardFlag: props.rewardFlag,
  idList: []
})

const conditionComponents = computed(() => [
  {
    type: 1,
    key: 'departmentId',
    placeholder: '学院（部）',
    config: {
      options: dict[dictEnum.department]
    }
  },
  {
    type: 0,
    key: 'codeOrName',
    placeholder: '请输入工号/姓名',
  },
  {
    type: 0,
    key: 'projectCodeOrName',
    placeholder: '请输入项目编号/项目名称',
  },
  {
    type: 0,
    key: 'projectType',
    placeholder: '请输入项目类别',
  },
  {
    type: 1,
    key: 'recognitionLevel',
    placeholder: '认定等级',
    config: {
      options: dict[dictEnum.resultGuideStuSureLevel]
    }
  },
  {
    type: 0,
    key: 'approvedBy',
    placeholder: '请输入批准单位',
  },
  {
    type: 2,
    key: 'businessDateArray',
    width: '460px',
    config: {
      startPlaceholder: '结项开始时间',
      endPlaceholder: '结项结束时间',
    }
  },
  {
    type: 0,
    key: 'issuingUnit',
    placeholder: '请输入颁发单位',
  },
])

const tableRef = ref()
const total = ref(0)
const dataList = ref<GuidePageVO[]>([])
const columns = [
  {
    title: '序号',
    width: 48,
    dataIndex: 'index',
    formatter: (row: any, column: any, cellValue: any, index: number) => {
      return index + 1
    }
  },
  {
    title: '所属院校（部）',
    minWidth: 80,
    dataIndex: 'departmentName',
  },
  {
    title: '工号',
    minWidth: 60,
    dataIndex: 'accountCode',
  },
  {
    title: '姓名',
    minWidth: 60,
    dataIndex: 'accountName',
  },
  {
    title: '项目编号',
    minWidth: 60,
    dataIndex: 'projectCode',
  },
  {
    title: '项目名称',
    minWidth: 100,
    dataIndex: 'name',
  },
  {
    title: '项目类别',
    minWidth: 60,
    dataIndex: 'projectType',
  },
  {
    title: '批准单位',
    minWidth: 60,
    dataIndex: 'approvedBy',
  },
  {
    title: '结项时间',
    minWidth: 100,
    dataIndex: 'businessDate',
    formatter: (row) => formateDate(row.businessDate).format('YYYY-MM-DD')
  },
  {
    title: '鉴定颁发单位',
    minWidth: 80,
    dataIndex: 'issuingUnit',
  },
  {
    title: '认定等级',
    minWidth: 60,
    dataIndex: 'recognitionLevelName',
  },
  {
    title: '科研分',
    minWidth: 60,
    dataIndex: 'rewardScore',
    formatter: (row) => props.rewardFlag ? row.rewardScore : row.researchScore
  },
  {
    title: '操作',
    minWidth: 60,
    dataIndex: 'default',
    slotName: 'default',
  }
]

const { run: getData, loading } = useRequest(getGuideStuListApi, {
  manual: true,
  onSuccess: (res: PageBeanGuidePageVO) => {
    const { content, totalElements } = res || {}
    dataList.value = content || []
    total.value = totalElements || 0
  }
})

/* 导出列表数据 */
const { runAsync: exportFile } = useRequest(exportGuideStuApi, {
  // false 为自动执行
  manual: true,
  // 请求成功
  onSuccess: () => {
  },
  // 请求失败
  onError: () => {
  },
})
// 导出
const exportListData = async (params) => {
  downloadParameters.value.idList = params
  const res = await exportFile({
    ...downloadParameters.value,
    ...searchBarRef.value.params,
    businessStartDate: parameters.value.businessDateArray?.[0],
    businessEndDate: parameters.value.businessDateArray?.[1],
  })
  downloadFile(res)
}

const refreshTableData = (params) => {
  parameters.value = { ...parameters.value, ...params }
  getData({
    ...parameters.value,
    status: 5,
    rewardFlag: props.rewardFlag,
    researchScoreFlag: true,
    statisticalYear: props.year,
    departmentId: parameters.value.departmentId,
    codeOrName: parameters.value.codeOrName,
    businessStartDate: parameters.value.businessDateArray?.[0],
    businessEndDate: parameters.value.businessDateArray?.[1],
    recognitionLevel: parameters.value.recognitionLevel,
    approvedBy: parameters.value.approvedBy,
    issuingUnit: parameters.value.issuingUnit
  })
}

const handleDetail = (id: string) => {
  router.push({ path: '/resultsManagement/office/office-guide-student/detail', query: { id, mode: 'preview' } })
}

onMounted(() => {
  tableRef.value?.refreshInitPage()
})
</script>

<style scoped lang="scss">
.person-summary-container {
  --at-apply: flex flex-col space-y-[16px] h-full;

  .search {
    --at-apply: px-[20px] py-[16px] bg-white rounded-[4px];
  }

  .list {
    --at-apply: flex-1 px-[20px] pb-[20px] bg-white rounded-[4px];
  }
}
</style>